//
//  MainViewController.m
//  TianyaQing
//
//  Created by gzty1 on 12-3-6.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "ClassificationViewController.h"
#import "HistoryListViewController.h"
#import "BadgeButton.h"
#import "UIButton+Custom.h"
#import "tyconsts.h"

static NSString* const KNotificationShowNewFlag=@"KNotificationShowNewFlag";//在主页下显示new

@interface MainViewController(Private)
- (UIView*)createSelfTabBar;
- (UIImage*)getTabBarItemBgImage:(int)index Selected:(BOOL)stauts;
- (void)showSelectedBarImage:(NSInteger)index;
- (void)showNewFlagInHomePage:(NSNotification*)notify;
@end

@implementation MainViewController

- (id)init
{
    if(self=[super init])
	{
        HomeViewController* homeCtrl = [[HomeViewController alloc] init];
        homeCtrl.categoryId=@"feeling";
        homeCtrl.categoryName=@"情感专区";
        homeCtrl.navigationItem.title=@"情感天地";
        homeCtrl.isRecommend=FALSE;
        homeCtrl.isFirstTab=TRUE;
		homeNav_ = [[CustomNavigationController alloc] initWithRootViewController:homeCtrl];
        [homeCtrl release];
		UITabBarItem* newsitem = [[UITabBarItem alloc] initWithTitle:@"主页" image:[UIImage imageNamed:@"home.png"] tag:1];  
        homeNav_.tabBarItem = newsitem;
		[newsitem release];
		//homeNav_.transitionType=1;
        
        HomeViewController* recommendCtrl = [[HomeViewController alloc] init];
        recommendCtrl.categoryId=@"feeling";
        recommendCtrl.categoryName=@"情感专区";
        recommendCtrl.navigationItem.title=@"情感推荐";
        recommendCtrl.isRecommend=TRUE;
		recommendNav_ = [[CustomNavigationController alloc] initWithRootViewController:recommendCtrl];
        [recommendCtrl release];
		UITabBarItem *discoveritem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"recommend.png"] tag:1];  
        recommendNav_.tabBarItem = discoveritem;
		[discoveritem release];
        //recommendNav_.transitionType=1;
		
        
        ClassificationViewController* classCtrl = [[ClassificationViewController alloc] init];
		classificationNav_ = [[CustomNavigationController alloc] initWithRootViewController:classCtrl];
        [classCtrl release];
		UITabBarItem* forumitem = [[UITabBarItem alloc] initWithTitle:@"分类" image:[UIImage imageNamed:@"classification.png"] tag:1];  
        classificationNav_.tabBarItem = forumitem;
		[forumitem release];
        //classificationNav_.transitionType=1;
        
        HistoryListViewController* historyCtrl = [[HistoryListViewController alloc] init];
		historyNav_ = [[CustomNavigationController alloc] initWithRootViewController:historyCtrl];
        [historyCtrl release];
		UITabBarItem* profileitem = [[UITabBarItem alloc] initWithTitle:@"历史" image:[UIImage imageNamed:@"history.png"] tag:1];  
        historyNav_.tabBarItem = profileitem;
		[profileitem release];
        //settingNav_.transitionType=1;
        
		self.viewControllers = [NSArray arrayWithObjects:homeNav_,recommendNav_,classificationNav_,historyNav_, nil];

        
        preSelectedIndex = 0;
        
        
        UIView *selfBarView=[self createSelfTabBar];
        [self.tabBar addSubview:selfBarView];
        
        [self setSelectedIndex:preSelectedIndex];
        [self showSelectedBarImage:preSelectedIndex];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showNewFlagInHomePage:) name:KNotificationShowNewFlag object:nil];
	}
	return self;
}

- (BadgeButton*)createSelfBarItemView:(NSString*)title withIndex:(int)index
{
    int width = [self tabBar].frame.size.width/[self.viewControllers count];
    CGRect rect = CGRectMake(width*index, 1, width, self.tabBar.frame.size.height);
    BadgeButton* itemBtn = [BadgeButton buttonWithType:UIButtonTypeCustom];
    itemBtn.tag=index+1000;
    itemBtn.frame=rect;
    UIImage* img = [self getTabBarItemBgImage:index Selected:FALSE];
    [itemBtn setBackgroundImage:img forState:UIControlStateNormal];
    UITabBarItem *item = [[self.tabBar items] objectAtIndex:index];
    [itemBtn setImage:item.image forState:UIControlStateNormal];
    [itemBtn setTitle:title forState:UIControlStateNormal];
    [itemBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:10]];
    itemBtn.badgeEdgeInsets=UIEdgeInsetsMake(5,0,0,10);
    [itemBtn centerImageAndTitle];
    [itemBtn addTarget:self action:@selector(tabButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    return itemBtn;
}

- (void)tabButtonPress:(id)sender
{
    int tag=[sender tag];
    if (1000+preSelectedIndex != tag)
    {
        //切换到其他界面
        self.selectedIndex=tag-1000;
		[self showSelectedBarImage:self.selectedIndex];
	}
    else
    {
        UIViewController* ctrl=nil;
        if (preSelectedIndex==0)
        {
            ctrl=[homeNav_ topViewController];
        }
        else if (preSelectedIndex==1)
        {
            ctrl=[recommendNav_ topViewController];
        }
        if (ctrl && [ctrl isKindOfClass:[HomeViewController class]])
        {
            [(HomeViewController*)ctrl tabClickAgain];
        }
    }
}

- (UIView*)createSelfTabBar
{
    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,self.tabBar.frame.size.width,self.tabBar.frame.size.height)];
    backView.userInteractionEnabled=TRUE;
	backView.contentMode = UIViewContentModeScaleToFill;
    backView.image=[UIImage imageNamed:@"tab_bg.png"];
    BadgeButton* itemBtn = [self createSelfBarItemView:@"主页" withIndex:0];
    [backView addSubview:itemBtn];
    itemBtn = [self createSelfBarItemView:@"推荐" withIndex:1];
    [backView addSubview:itemBtn];
    itemBtn = [self createSelfBarItemView:@"分类" withIndex:2];
    [backView addSubview:itemBtn];
    itemBtn = [self createSelfBarItemView:@"历史" withIndex:3];
    [backView addSubview:itemBtn];
	return [backView autorelease];
}

- (UIImage*)getTabBarItemBgImage:(int)index Selected:(BOOL)stauts
{
    if (stauts)
    {
        return [UIImage imageNamed:@"item_sel_bg.png"];
    }
    return nil;
}

- (void)showSelectedBarImage:(NSInteger)index
{
    if (preSelectedIndex>=0 && preSelectedIndex!=index) 
    {
        BadgeButton* itemBtn = (BadgeButton*)[[self tabBar] viewWithTag:1000+preSelectedIndex];
        [itemBtn setBackgroundImage:[self getTabBarItemBgImage:preSelectedIndex Selected:FALSE] forState:UIControlStateNormal];
        [itemBtn setNeedsLayout];
    }
    preSelectedIndex = index;
    
    BadgeButton* itemBtn = (BadgeButton*)[[self tabBar] viewWithTag:1000+index];
    if (itemBtn) 
    {
        [itemBtn setBackgroundImage:[self getTabBarItemBgImage:index Selected:TRUE] forState:UIControlStateNormal];
        [itemBtn setNeedsLayout];
    }
}

- (void)showNewFlagInHomePage:(NSNotification*)notify
{
    BadgeButton* itemBtn = (BadgeButton*)[[self tabBar] viewWithTag:1000];
    if (itemBtn) 
    {
        itemBtn.badgeEdgeInsets=UIEdgeInsetsMake(5, 0, 0, 5);
        NSDictionary*paramDict = (NSDictionary*)[notify object];//获取到传递的对象
        NSString* value=[paramDict valueForKey:@"new"];
        if (value && [value isEqualToString:@"1"])
        {
            UIImageView* newImage=(UIImageView*)[itemBtn viewWithTag:998];
            if (!newImage)
            {
                UIImage* img=[UIImage imageNamed:@"new.png"];
                newImage=[[UIImageView alloc] initWithImage:img];
                newImage.frame=CGRectMake(itemBtn.frame.size.width-img.size.width-3, 3, img.size.width, img.size.height);
                newImage.tag=998;
                [itemBtn addSubview:newImage];
                [newImage release];
            }
        }
        else
        {
            UIView* newImage=[itemBtn viewWithTag:998];
            if (newImage)
            {
                [newImage removeFromSuperview];
            }
        }
        [itemBtn setNeedsLayout];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)dealloc 
{
    [homeNav_ release];
    [recommendNav_ release];
    [classificationNav_ release];
    [historyNav_ release];
	[super dealloc];
}

@end
